<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

abstract class m_backoffice_extends_BlockEditModal extends f_core_Action
{
    public function saveBlockAndClose()
    {
        $page = $this->getPage();
		$blockParameters = $page->getBlockById($this->getBlockParameter('blockId'));

        $forbidden = array('pageId', 'pageLang', 'blockId', 'blockLabel', 'blockIcon', 'blockLang');
        
        foreach($this->getFormValues('form') as $k => $v)
        {
            if(!in_array($k, $forbidden))
            {
                $blockParameters[$k] = $v;
            }
        }
        $page = $this->getPage();
        $page->setBlockParamsById($blockParameters['blockId'], $blockParameters);
        m_backoffice_services_TempDocService::getInstance()->saveTemporaryDocument($page);
        
        $this->closeModal();
    }

    public function saveBlock($blockParams)
    {   
        $blockId = $this->getBlockParameter('blockId');
        $page = $this->getPage();
		$block = $page->getBlockById($blockId);

		foreach($blockParams as $key => $value)
		{
			if($key != "pageId" && $key != "blockId")
			{
				if(is_array($value))
				{
					if(isset($value["vide"]) && !empty($value[0]))
					{
						unset($value["vide"]);
					}
					$block[$key] = join(',',$value);
				}
				else
				{
					$block[$key] = stripslashes($value);
				}
			}
		}
        
        $page->setBlockParamsById($blockId, $block);

        m_backoffice_services_TempDocService::getInstance()->saveTemporaryDocument($page);
    }

    public function getBlock()
    {
        $page = $this->getPage();
        return $page->getBlockById($this->getRequestParameter('blockId'));
    }

    /**
     * Get current website
     *
     * @return m_website_documents_website
     */
    public function getWebsite()
    {
        return $this->getPage()->getWebsite();
    }

    /**
     * Get current edited page
     *
     * @return m_website_documents_page
     */
    public function getPage()
    {
        $tempDocService = m_backoffice_services_TempDocService::getInstance();
        return $tempDocService->getDocument($this->getRequestParameter('docId'), $this->getRequestParameter('docLang'));
    }

    public function getPageSelectBox($selectedId = null)
    {
        if(is_null($selectedId))
        {
            $selectedId = $this->getPage()->getUniqueId();
        }

        return m_backoffice_services_TreeService::getInstance()->getSelectArray($this->getWebsite()->getUniqueId(), 'modules_website/page', $selectedId, false, true, 'website');
    }

    public function isFormSubmit()
    {
        return (count($this->getFormValues('form')) > 0);
    }

    /**
     * Set parameters for modal template
     */
    public function setModalParameters()
    {
        $parameters = $this->getRequest()->getParameters();

        if(is_array($parameters['href']))
        {
            $parameters['href'] = '?module=backoffice&action=AjaxContext&href='.$parameters['href'][0];
        }

        $blockContext = $this->getBlockContext()->getParameters();

        if(!is_null($blockContext))
        {
            $parameters['label'] = $blockContext['blockLabel'];
        }

        $this->addToModel('blockParams', $blockContext);
        $this->addToModel('modal', $parameters);
    }

    /**
     * Close modal and refresh page edition
     */
    public function closeModal()
    {
        die('<div id="formsuccess" style="display:none">blockEditRefresh</div>');
    }

    /**
     * Default validate action
     * @return boolean
     */
    public function validate()
    {
        return $this->getUserContext()->hasRight("backoffice_accessBo");
    }

    /**
     * Default errorHandler
     * @return string
     */
    public function errorHandler()
    {
        return f_template_Template::getInstance()->setModuleTemplate('backoffice', 'block/modal/ModalNoRights.html')->render();
    }
}